#!/usr/bin/env python
#  Copyright (c) 2010
#  The Regents of the University of Michigan
#  All Rights Reserved

#  Permission is granted to use, copy, create derivative works, and
#  redistribute this software and such derivative works for any purpose,
#  so long as the name of the University of Michigan is not used in
#  any advertising or publicity pertaining to the use or distribution
#  of this software without specific, written prior authorization. If
#  the above copyright notice or any other identification of the
#  University of Michigan is included in any copy of any portion of
#  this software, then the disclaimer below must also be included.

#  This software is provided as is, without representation or warranty
#  of any kind either express or implied, including without limitation
#  the implied warranties of merchantability, fitness for a particular
#  purpose, or noninfringement.  The Regents of the University of
#  Michigan shall not be liable for any damages, including special,
#  indirect, incidental, or consequential damages, with respect to any
#  claim arising out of or in connection with the use of the software,
#  even if it has been or is hereafter advised of the possibility of
#  such damages.

import log_parser

import cPickle
import sys
import time

def main():
  TARGET_KEYS = dict(eval(open(sys.argv[1]).read()))
  pickle_file_name = '%s.pickle' % sys.argv[1]
  found_keys = cPickle.load(open(pickle_file_name, 'rb'))
  for log in sys.argv[2:]:
    for (key, store_time, version, value) in log_parser.get_stores(log):
      if key in TARGET_KEYS:
        if value == TARGET_KEYS[key]:
          found_keys.add(key)
#          print 'Key %s FOUND in %s, stored at %s' % (key.encode('hex'), log, store_time)
        else:
          pass
#          print 'Key %s found with wrong value %s instead of %s' %(key.encode('hex'), TARGET_KEYS[key].encode('hex'), value.encode('hex'))

  print 'Found a total of %d fragments so far' % len(found_keys)
  cPickle.dump(found_keys, open(pickle_file_name, 'wb'))
if __name__ == '__main__':
  main()
